package com.itheima.d4_algorithm;

/**
 * 目标：掌握二分查找算法。
 * <p>
 * binarySearch
 */
public class Test3 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int a = erfen(arr, 9);
        System.out.println(a);
    }

    public static int erfen(int[] arr, int num) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] > num) {
                right = mid - 1;
            } else if (arr[mid] < num) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
